package controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vo.ResponseEntity;

@Controller
@RequestMapping("cart")
public class CartController {
	
	
	/*
	 *购物车部分的增加减少功能，
	 *action , add 代表 添加， sub 代表 减少 
	 */
	@RequestMapping("dish")
	@ResponseBody
	public ResponseEntity addDish(String mid,String did,String action,HttpSession session){
		ResponseEntity resp = new ResponseEntity();
		Map<String,Map<String,Integer>> carts = (Map<String,Map<String,Integer>>) session.getAttribute("carts");
		if(carts == null)
			carts = new HashMap<String,Map<String,Integer>>();
		Map<String,Integer> dishCarts = carts.get(mid);
		if(dishCarts == null)
			dishCarts = new HashMap<String,Integer>();
		System.out.println("before===" + dishCarts.size() + "===");
		if(action.equals("add")){
			if(dishCarts.containsKey(did)){
				int count = dishCarts.get(did);
				dishCarts.put(did, count + 1);
				resp.setStatus(true);
				resp.setMessage("update");
				System.out.println("商家" + mid + "的购物车为：此时菜品 " + did + "的数量为：" + count +",增加后的数据为：" + (count+1) );
			}else{
				resp.setStatus(true);
				dishCarts.put(did, 1);
				resp.setMessage("add");
				System.out.println("添加了一个菜品，菜品的ID为：" + did);
			}
		}else if(action.equals("sub")){
			if(dishCarts.containsKey(did)){
				int count = dishCarts.get(did) - 1;
				if(count <= 0){
					System.out.println("数量为:" + count + ",则在session中删除;");
					dishCarts.remove(did);
				}else{
					dishCarts.put(did, count);
				}
				resp.setStatus(true);
				resp.setMessage("sub success");
			}
		}
		carts.put(mid, dishCarts);
		System.out.println("after===" + dishCarts.size() + "===");
		session.setAttribute("carts", carts);
		
		for(String id:dishCarts.keySet()){
			System.out.println("菜品id:" + id + ",数量为:" + dishCarts.get(id));
		}
		return resp;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public ResponseEntity updateDish(String mid,String did,Integer count,HttpSession session){
		ResponseEntity resp = new ResponseEntity();
		Map<String,Map<String,Integer>> carts = (Map<String,Map<String,Integer>>) session.getAttribute("carts");
		if(carts == null)
			carts = new HashMap<String,Map<String,Integer>>();
		Map<String,Integer> dishCarts = carts.get(mid);
		if(dishCarts == null)
			dishCarts = new HashMap<String,Integer>();
		System.out.println("before===" + dishCarts.size() + "===");
		if(count <= 0){
			dishCarts.remove(did);
		}else{
			dishCarts.put(did, count);
			System.out.println(dishCarts.get(did));
		}
		carts.put(mid,dishCarts);
		session.setAttribute("carts", carts);
		System.out.println("now the mid is :" + mid);
		for(String id:dishCarts.keySet()){
			System.out.println("菜品id:" + id + ",数量为:" + dishCarts.get(id));
		}
		resp.setStatus(true);
		resp.setMessage("update success!");
		
		
		return resp;
	}
}
